// 全局异常处理切面
package com.fjnu.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class GlobalExceptionAspect {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionAspect.class);

    @Around("@within(com.fjnu.annotation.ExceptionGlobalCatch)")
    public Object handleException(ProceedingJoinPoint joinPoint) {
        try {
            return joinPoint.proceed();
        } catch (Throwable e) {
            logger.error("Service层异常 - 方法: {}, 异常: {}",
                    joinPoint.getSignature().getName(), e.getMessage(), e);
            // 根据异常类型返回不同的错误码
            if (e instanceof IllegalArgumentException) {
                return 0; // 参数错误
            } else if (e instanceof RuntimeException) {
                return 0; // 业务异常
            } else {
                return 0; // 系统异常
            }
        }
    }
}